
SControllers.controller('index_ctrl', ['$scope', '$http', '$timeout', '$upload',
  function($scope, $http, $timeout, $upload) {

    $scope.plugin_info = [];
    $scope.upload_percent = 0;
    $scope.upload_success = false;
    $scope.uploadfiles = [];
    $scope.crawl_query_result_list = [];
    $scope.find_crawl_query = "";

    $scope.find_crawl = function() {
        if ($scope.find_crawl_query === undefined || $scope.find_crawl_query === "")
            return;

        var url = "/spiderkit/flowcrawl_info?pluginNameQuery=" + $scope.find_crawl_query;

        $http.get(url).success(
            function(data) {
                console.log(data);
                if (data.length == 0) {
                    showMessage("Can't find " + $scope.find_crawl_query);
                } else {
                    $scope.crawl_query_result_list = data;
                    $('#crawlFindResultModal').modal();
                }
            }
        );
    };
    
    $scope.update_plugin_info = function() {
        var url = "/spiderkit/get_plugin_info";
        $http.get(url).success( 
            function(data) {
                $scope.plugin_info = data;
            }
        );
    };

    $scope.on_fileselect = function($files) {
        $scope.upload_percent = 0;
        $scope.uploadfiles = $files;
    };

    $scope.plugin_file_exists = function() {
        for (var i = 0; i < $scope.uploadfiles.length; i++) {
            if ($scope.uploadfiles[i] === undefined || $scope.uploadfiles[i].name === "") {
                continue;
            }

            var url = "/spiderkit/plugin_manager?action=fileExists&pluginFileName="+$scope.uploadfiles[i].name;
            $http.get(url).success(
                function(data) {
                    if (data["exists"]) {
                        $('#fileExistsModal').modal();
                    } else {
                        $scope.on_upload();
                    }
                }
            );
        }
    }

    $scope.on_upload = function() {
        for (var i = 0; i < $scope.uploadfiles.length; i++) {
            var file = $scope.uploadfiles[i];
            $scope.upload = $upload.upload({
                url: 'upload',
                data: {myObj: $scope.myModelObj},
                file: file,
            }).progress(function(evt) {
                $scope.upload_percent = parseInt(100.0 * evt.loaded / evt.total);
                console.log('percent: ' + parseInt(100.0 * evt.loaded / evt.total));
            }).success(function(data, status, headers, config) {
                console.log(data);
                $scope.update_plugin_info();
            });
        }
    };
      
    $scope.on_collapse_click = function(target) {
        $("#"+target.getAttribute("cdata")).collapse('toggle');
    };

    var message_modal_show = false;
    $('#messageModal').on('show.bs.modal', function (e) {
        message_modal_show = true;
    });

    $('#messageModal').on('hide.bs.modal', function (e) {
        message_modal_show = false;
    });

    function showMessage(text) {
        $('#message').html(text);
        if (!message_modal_show) {
            $('#messageModal').modal({keyboard: false});
        }
    }

    $scope.update_plugin_info();
  }]
);


